import maya.cmds as mc
import maya.mel as mel
import maya.OpenMaya as OpenMaya
import re
#import myPublicClass.ofRig_mainClass as MC
#import myPublicClass.ofRig_allCtrl as AC
#mcInstance = MC.ofRig_MainClass()
#acInstance = AC.ofRig_allCtrl()
###########
#author:baoran
#data:2013 3 21
###########
class CreateVis(object):
    def __init__(self,setName = 'set_ctrl'):
        self.setName = setName
    def UI(self):
        visUI = 'visUi'
        if(mc.window(visUI,ex = True)):
            mc.deleteUI(visUI,window = True)
        else:
            pass
        mc.window(visUI,t = 'createVIS',s = False,tb = True,widthHeight=(300, 100))
        mc.rowLayout(numberOfColumns=3,columnWidth3=(100,100,100), adjustableColumn=1, columnAlign=(1, 'left'), columnAttach=[(1, 'left', 1),\
         (1, 'both', 0), (2, 'both', 0)])
        mc.text('CharacterName:')
        mc.textField('CN')
        mc.button(l='Create',c= lambda i:self.ui_createVis())
        mc.showWindow( visUI )
        
    def ui_createVis(self):
        textCtrl = mc.textField('CN',q=True,text=True)
        self.createVis(charactor=textCtrl)
    def lockAttr(self,obj = '' ,attr =['']):


        for at in attr:

                mc.setAttr(obj+'.'+ at, lock=True ,keyable = False ,channelBox = False)
                
    def createVis(self , charactor = '' ):
        textName = charactor
        if textName != '':
            ###create text
            tempObj = mc.textCurves(ch=False,f = 'Times New Roman|h-13|w400|c0',t = textName)[0]
            TextCurve = mc.ls('makeTextCurves*')
            mc.delete(TextCurve)
            mc.makeIdentity(tempObj,apply=True,t=True,r=True,s=True)

            
            allObj = mc.listRelatives(tempObj,ad=True)
            allObj.append(tempObj)
            shapeList = []
            for obj in allObj:
                if mc.nodeType( obj ) == 'nurbsCurve':
                    shapeList.append(obj)
                else:
                    pass

            
            all_ctrl = mc.group(em=True,n = 'visibility_ctrl')
            
            for i,shape in enumerate(shapeList):
                

                    
                    mc.parent(shape,all_ctrl,r=True,s=True)

                    mc.setAttr(shape+'.overrideEnabled',1)
                    mc.setAttr(shape+'.overrideColor',17)
            mc.delete(tempObj)


            ctrlShape = mc.listRelatives(all_ctrl,s=True)
            for i,s in enumerate(ctrlShape):
                mc.rename(s,all_ctrl+str(i+1)+'Shape')

            self.lockAttr( obj = all_ctrl,attr = ['tx','ty','tz','rx','ry','rz','sx','sy','sz','v'])
            

            mc.addAttr(all_ctrl,ln = 'JntVis',at = 'long',k=True,dv=1,min=0,max=1)
            mc.setAttr(all_ctrl+'.JntVis',k=False,cb=True)

            mc.addAttr(all_ctrl,ln = 'CtrlVis',at = 'long',k=True,dv=1,min=0,max=1)
            mc.setAttr(all_ctrl+'.CtrlVis',k=False,cb=True)

            mc.addAttr(all_ctrl,ln = 'mode',at = 'double',k=True,dv=0,min=0,max=10)
            mc.setAttr(all_ctrl+'.mode',k=False,cb=True)
            

            

            mc.addAttr(all_ctrl,ln = 'FaceMode',at = 'enum',en = 'High:Low',k=True)
            mc.setAttr(all_ctrl+'.FaceMode',k=False,cb=True)

            mc.addAttr(all_ctrl,ln = 'BodyMode',at = 'enum',en = 'High:Low',k=True)
            mc.setAttr(all_ctrl+'.BodyMode',k=False,cb=True)

            mc.addAttr(all_ctrl,ln = 'FacialPanel',at = 'enum',en = 'On:Off',k=True)
            mc.setAttr(all_ctrl+'.FacialPanel',k=False,cb=True)

            mc.addAttr(all_ctrl,ln = 'meshDisplayType',at = 'enum',en = 'Normal:Template:refrence',k=True)
            mc.setAttr(all_ctrl+'.meshDisplayType',k=False,cb=True)
            
            
            self.normalizeHull( curvename = all_ctrl )
            self.scaleCurveHull( curvename = all_ctrl ,scalX =5,scalY=5,scalZ=5)
            all_ctrlZero = str(all_ctrl)+'_zero'
            mc.group(all_ctrl,n= all_ctrlZero )
            mc.setAttr( str(all_ctrlZero)+'.translateY',200)
        else:
            all_ctrlZero = ''
        return all_ctrlZero
                
        
    def connectVisAttr(self):
        ####jnt vis
        visCtrl = 'of3d_rig_ctrl'
        hideJntAttr = 'globalctrl_grp.hideJnt'
        ctrlSet = 'set_ctrl'
        if mc.objExists(hideJntAttr):
            mc.connectAttr(visCtrl+'.jntHide',hideJntAttr)
            mc.setAttr(hideJntAttr,l=True,k=False)
        else:
            pass
        ###mesh vis
        meshList = mc.ls(type = 'mesh')
        if meshList != []:
            for mesh in meshList:
                mc.setAttr(mesh+'.overrideEnabled',1)
                mc.connectAttr(visCtrl+'.meshDisplayType',mesh+'.overrideDisplayType')
        else:
            pass
        ### ctrl vis
        nurvesList = mc.ls(type = 'nurbsCurve')
        for nurve in nurvesList:
            connObj = mc.listConnections(nurve+'.v')
            visNum = mc.getAttr(nurve+'.v')
            if connObj == None and 'of3d_rig_ctrl' not in nurve and visNum == 1:
                mc.connectAttr(visCtrl+'.ctrlVis',nurve+'.v')
        annotationList = mc.ls(type = 'annotationShape')
        if annotationList != []:
           for ann in annotationList:
               mc.connectAttr(visCtrl+'.ctrlVis',ann+'.v')    
    #######
    
    
    
    def rotateCurveHull(self, curveName = '',rx = 0,ry = 0, rz = 0 ,centerP = 1 ):
        curveshapes=mc.listRelatives(curveName,s=True)
        
        cvAllX=[]
        cvAllY=[]
        cvAllZ=[]

        for curveshape in curveshapes:
            spans=mc.getAttr(str(curveshape)+'.spans')
            degree=mc.getAttr(str(curveshape)+'.degree')
            pointsMounts=spans+degree

            for i in range(pointsMounts):

                pointcvPos=mc.xform(str(curveshape)+'.cv['+str(i)+']',q=True,t=True,ws=True)

                cvAllX.append(pointcvPos[0])
                cvAllY.append(pointcvPos[1])
                cvAllZ.append(pointcvPos[2])
                
        pointCVposXMin=cvAllX[0]
        pointCVposYMin=cvAllY[0]
        pointCVposZMin=cvAllZ[0]
        
        pointCVposXMax=cvAllX[0]
        pointCVposYMax=cvAllY[0]
        pointCVposZMax=cvAllZ[0]
        


        for k in range(len(cvAllX)):
            pointCVposXMin=min(pointCVposXMin,cvAllX[k])
            pointCVposXMax=max(pointCVposXMax,cvAllX[k])
            
            pointCVposYMin=min(pointCVposYMin,cvAllY[k])
            pointCVposYMax=max(pointCVposYMax,cvAllY[k])
            
            pointCVposZMin=min(pointCVposZMin,cvAllZ[k])
            pointCVposZMax=max(pointCVposZMax,cvAllZ[k])
        
        centerPivetX=(pointCVposXMin+pointCVposXMax)/2
        centerPivetY=(pointCVposYMin+pointCVposYMax)/2
        centerPivetZ=(pointCVposZMin+pointCVposZMax)/2
        
        if centerP==1:
            centerPivet=[centerPivetX,centerPivetY,centerPivetZ]
            
        else:
            centerPivet = mc.xform(curveName , q=True ,t =True,ws=True)
        mc.rotate( rx,ry,rz,str(curveshape) + '.cv[0:' + str(pointsMounts)  +']' ,os=True, r=True,p = centerPivet )
            
        
    def scaleCurveHull(self,curvename,scalX,scalY,scalZ):
        curveshapes=mc.listRelatives(curvename,s=True)
        
        cvAllX=[]
        cvAllY=[]
        cvAllZ=[]

        for curveshape in curveshapes:
            spans=mc.getAttr(str(curveshape)+'.spans')
            degree=mc.getAttr(str(curveshape)+'.degree')
            pointsMounts=spans+degree

            for i in range(pointsMounts):

                pointcvPos=mc.xform(str(curveshape)+'.cv['+str(i)+']',q=True,t=True,ws=True)

                cvAllX.append(pointcvPos[0])
                cvAllY.append(pointcvPos[1])
                cvAllZ.append(pointcvPos[2])
                
        pointCVposXMin=cvAllX[0]
        pointCVposYMin=cvAllY[0]
        pointCVposZMin=cvAllZ[0]
        
        pointCVposXMax=cvAllX[0]
        pointCVposYMax=cvAllY[0]
        pointCVposZMax=cvAllZ[0]
        


        for k in range(len(cvAllX)):
            pointCVposXMin=min(pointCVposXMin,cvAllX[k])
            pointCVposXMax=max(pointCVposXMax,cvAllX[k])
            
            pointCVposYMin=min(pointCVposYMin,cvAllY[k])
            pointCVposYMax=max(pointCVposYMax,cvAllY[k])
            
            pointCVposZMin=min(pointCVposZMin,cvAllZ[k])
            pointCVposZMax=max(pointCVposZMax,cvAllZ[k])
        
        centerPivetX=(pointCVposXMin+pointCVposXMax)/2
        centerPivetY=(pointCVposYMin+pointCVposYMax)/2
        centerPivetZ=(pointCVposZMin+pointCVposZMax)/2
        
        centerPivet=[centerPivetX,centerPivetY,centerPivetZ]
            
        #mc.select(str(curvename)+'.cv[0:'+str((pointsMounts-1))+']')
        
        for curveshape in curveshapes:
            
            spans=mc.getAttr(str(curveshape)+'.spans')
            degree=mc.getAttr(str(curveshape)+'.degree')
            pointsMounts=spans+degree
            
            
            cv = str(curveshape)+'.cv[0:'+str((pointsMounts-1))+']'
            mc.scale(scalX,scalY,scalZ,cv,r=True,p=centerPivet)
            
    def normalizeHull(self,curvename):
        curveshapes=mc.listRelatives(curvename,s=True)
        
        cvAllX=[]
        cvAllY=[]
        cvAllZ=[]

        for curveshape in curveshapes:
            spans=mc.getAttr(str(curveshape)+'.spans')
            degree=mc.getAttr(str(curveshape)+'.degree')
            pointsMounts=spans+degree

            for i in range(pointsMounts):

                pointcvPos=mc.xform(str(curveshape)+'.cv['+str(i)+']',q=True,t=True,ws=True)

                cvAllX.append(pointcvPos[0])
                cvAllY.append(pointcvPos[1])
                cvAllZ.append(pointcvPos[2])
                
        pointCVposXMin=cvAllX[0]
        pointCVposYMin=cvAllY[0]
        pointCVposZMin=cvAllZ[0]
        
        pointCVposXMax=cvAllX[0]
        pointCVposYMax=cvAllY[0]
        pointCVposZMax=cvAllZ[0]
        


        for k in range(len(cvAllX)):
            pointCVposXMin=min(pointCVposXMin,cvAllX[k])
            pointCVposXMax=max(pointCVposXMax,cvAllX[k])
            
            pointCVposYMin=min(pointCVposYMin,cvAllY[k])
            pointCVposYMax=max(pointCVposYMax,cvAllY[k])
            
            pointCVposZMin=min(pointCVposZMin,cvAllZ[k])
            pointCVposZMax=max(pointCVposZMax,cvAllZ[k])
        
        centerPivetX=(pointCVposXMin+pointCVposXMax)/2
        centerPivetY=(pointCVposYMin+pointCVposYMax)/2
        centerPivetZ=(pointCVposZMin+pointCVposZMax)/2
        
        centerPivet=[centerPivetX,centerPivetY,centerPivetZ]
        

        
        position = mc.xform( curvename ,q=True ,t=True,ws =True)
        for curveshape in curveshapes:
            
            spans=mc.getAttr(str(curveshape)+'.spans')
            degree=mc.getAttr(str(curveshape)+'.degree')
            pointsMounts=spans+degree
            
            
            cv = str(curveshape)+'.cv[0:'+str((pointsMounts-1))+']'
            mc.move( ( position[0] -centerPivet[0] ),( position[1] -centerPivet[1]   ),(  position[2]-centerPivet[2] ), cv , r=True)
    
    
#aa = CreateVis()
#aa.UI()